package com.ig507.util;

import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import com.ig507.constant.Ig507ErrorContent;

/**
 * 实例化一些工具类对象，让其他类直接继承这个对象即可使用这些工具类对象
 * 
 */
public class UtilFather {

	/**
	 * json处理类
	 */
	public static final JsonMapper jsonMapper = new JsonMapper();

	/**
	 * 初始化Jsoup
	 */
	public static Document getPage(String url) {
		Document doc = null;
		try {
			Connection connection = Jsoup.connect(url);

			connection = connection
					.header("Accept",
							"text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8")
					.header("Accept-Encoding", "gzip, deflate, br")
					.header("Accept-Language", "zh-CN,zh;q=0.9,en-US;q=0.8,en;q=0.7")
					.header("Cache-Control", "max-age=0").header("Connection", "keep-alive")
					.header("Host", "hq.sinajs.cn").header("Upgrade-Insecure-Requests", "1")
					.userAgent(
							"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.149 Safari/537.36")
					.referrer(url).ignoreContentType(true).ignoreHttpErrors(true).timeout(60000);

			doc = connection.get();

		} catch (Exception e) {
			e.printStackTrace();
		}
		return doc;
	}

	public static Document getIg507Page(String url) {
		Document doc = getPage(url);
		while (doc.text().equals(Ig507ErrorContent.E_503) || doc.text().equals(Ig507ErrorContent.E_BASE_LICENCE)
				|| doc.text().equals(Ig507ErrorContent.E_LICENCE_LIMIT)) {
			doc = getPage(url);
		}
		return doc;
	}

}
